module(..., package.seeall)


Switch = class("Switch")

function Switch.__init(self, enabled)
    self.enabled = false
    self:set_enabled(enabled)
end

function Switch.__tostring(self)
    return ("<%s %s>"):format(self.__name, self.enabled and "enabled" or "disabled")
end

function Switch.enable(self)
    if not self.enabled then
        self.enabled = true
        local enable = self._enable
        if type(enable) == "function" then 
            enable(self)
        end
    end
end

function Switch.disable(self)
    if self.enabled then
        self.enabled = false
        local disable = self._disable
        if type(disable) == "function" then 
            disable(self)
        end
    end
end

function Switch.set_enabled(self, enabled)
    (enabled and self.enable or self.disable)(self)
end

function Switch.toggle(self)
    self:set_enabled(not self.enabled)
end
